from xml.dom import minidom
import socket
import tkinter as tk
from tkinter import ttk
import xml

#open xml file
dom = xml.dom.minidom.parse('UNData.xml')

#get elements in the file
root = dom.documentElement

#search by Tag
cc = dom.getElementsByTagName('Country')

for c in cc:
    # elements of country
    country = []
    a = 0
    while a < len(cc):
        country.append(cc[a].firstChild.data)
        a+=28
ip_port = (socket.gethostname(), 5000)

s = socket.socket()     # 创建套接字

s.connect(ip_port)      # 连接服务器

while True:
    # use Tkinter , create a window
    win = tk.Tk()
    win.title("Select a country")  # Title of the window
    win.geometry("500x300+200+20")
    '''
    pull-down menu
    '''
    xVariable = tk.StringVar()

    # Create a pull-down menu
    com = ttk.Combobox(win, textvariable=xVariable)
    com.pack()
    com["value"] = country
    com.current(0)  # Set a default value

    com.bind("<<ComboboxSelected>>")
    country_select = com.get()
    p = country.index(country_select)
    win.destroy()

    inp = input("Are you sure to looking the data of",country_select,'?(yes/no)').strip()
    if inp == 'yes':     # 防止输入空信息，导致异常退出
        continue
    s.sendall(inp.encode())

    if inp == "exit":   # 如果输入的是‘exit’，表示断开连接
        print("You have exit.")
        break

    server_reply = s.recv(1024).decode()
    print(server_reply)

s.close()